package com.gitee.xhs.sampleRecod.transactionRecord;

import cn.hutool.core.date.DateUtil;
import com.github.jsonzou.jmockdata.JMockData;

import java.util.Date;

/**
 * @author BigHead
 * @date 2022/7/26 10:06
 * @description TransactionBySecondIterator
 */
public class TransactionBySecondIterator extends AbstractTransactionIterator {

    private final Integer groupNum;

    public TransactionBySecondIterator(boolean bounded, AbstractTransactionMockerInterceptor abstractTransactionMockerInterceptor, Integer maxBounded, Long initialTimeStamp, Long intervalTimeStamp, Boolean useOutTimeStamp, Integer groupNum) {
        super(bounded, abstractTransactionMockerInterceptor, maxBounded, initialTimeStamp, intervalTimeStamp, useOutTimeStamp);
        this.index = 1;
        this.groupNum = groupNum;
    }

    private Integer dataGroupNum() {
        if (useOutTimeStamp) {
            return DateUtil.date(new Date(iteratorTime(null))).second() % groupNum;
        } else {
            return DateUtil.date(new Date(timestamp)).second() % groupNum;
        }
    }

    @Override
    public boolean hasNext() {
        if (index < maxBounded) {
            return true;
        } else {
            return !bounded;
        }
    }

    @Override
    public Transaction next() {
        this.abstractTransactionMockerInterceptor.getDataNum(dataGroupNum() - 1);
        Transaction transaction = JMockData.mock(Transaction.class, this.abstractTransactionMockerInterceptor.getMockConfig());
        transaction.setDataNum(index++);
        if (this.useOutTimeStamp) {
            transaction.setTimestamp(iteratorTime(null));
        } else {
            transaction.setTimestamp(timestamp);
        }
        timestamp += intervalTimeStamp;
        return transaction;
    }
}
